package tgc.edu.exam.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import tgc.edu.exam.custom.CommonService;
import tgc.edu.exam.custom.TimeUtils;
import tgc.edu.exam.entity.CurriculumPlan;
import tgc.edu.exam.repository.CurriculumPlanRepository;
import tgc.edu.exam.web.form.CurriculumTableForm;

@Service
public class CurriculumPlanService extends CommonService<CurriculumPlan, UUID> {
	
	@Autowired
	private CurriculumPlanRepository cpRepository;

	public List<CurriculumPlan> findByTbClassId(UUID id) {
		return cpRepository.findByTbClassId(id);
	}

	public CurriculumPlan findByCurriculumCourseName(String string) {
		
		return cpRepository.findByCurriculumCourseName(string);
	}

	public List<CurriculumPlan> findByCurriculumIdAndTbClassClassStatusState(UUID id, String string) {
		return cpRepository.findByCurriculumIdAndTbClassClassStatusState(id, string);
	}

	/***
	 * 动态课表
	 * 数据整理
	 * @param clist
	 * @return 
	 */
	public List<CurriculumTableForm> findBydtkc(List<CurriculumPlan> clist) {
		SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd"); 
		String thistimedata = dateFormat.format(new Date());
		
		List<CurriculumTableForm> form = new ArrayList<>();
		/** 日期 **/
		String onedate = "";
		
		
		/** 参数二 存时间段 **/
		String twodate = "";
		
		/** 参数三 存时分秒 **/
		String threedate = "";
		
		for (int i = 0; i < clist.size(); i++) {
			CurriculumPlan c = clist.get(i);
			CurriculumTableForm table = new CurriculumTableForm();
			
			String Starttime = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(c.getStarttime());//开始时间
			String Finishttime = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(c.getFinishttime());//结束时间
			String Start[] = Starttime.split(" ");
			String Finisht[] = Finishttime.split(" ");
			/** 判断时间间隔是否相同的同一天 **/
			if(Start[0].equals(Finisht[0])) {
				threedate = "";
				if(!onedate.equals(Start[0]+"—"+Finisht[0])) {//不同的一天
					String tabledata = Start[0] +"<br/>("+TimeUtils.WeekDay(Start[0])+")";
					table.setData(tabledata);
					
					twodate = "";
				}
				String day = TimeUtils.getDay(Start[1],Finisht[1]);//取出day(上午下午)
				if(!twodate.equals(day)) {
					table.setDay(day);
					twodate = day;
				}
				if(Start[0].equals(thistimedata)) {
					table.setBackground(true);
				}
				table.setTime(Start[1]+"—"+Finisht[1]);
			}else {//处理不是同一天的情况，比如连续3天活动
				if(!onedate.equals(Start[0]+"—"+Finisht[0])) {
					String tabledata = Start[0] + "<br/>至<br/>" +Finisht[0];
					table.setData(tabledata);
					twodate = "";
				}
				
				String day = "全天";
				if(!twodate.equals(day)) {
					table.setDay(day);
					twodate = day;
				}
				if(!threedate.equals(Start[1]+"—"+Finisht[1])) {
					table.setTime(Start[1]+"—"+Finisht[1]);
					threedate = Start[1]+"—"+Finisht[1];
				}
				
				try {
					if(dateFormat.parse(Start[0]).getTime()<dateFormat.parse(thistimedata).getTime()&&dateFormat.parse(Finisht[0]).getTime()>dateFormat.parse(thistimedata).getTime()) {
						table.setBackground(true);
					}
				} catch (ParseException e) {
					
					e.printStackTrace();
				}
				
				
			}
			table.setKc(c.getCurriculum().getCourseName());
			table.setTeachername(c.getCurriculum().getTeacher().getName());
			form.add(table);
			onedate = Start[0]+"—"+Finisht[0];
		}
		return form;
	}

	/***
	 * 动态课程
	 * 数据处理
	 * @param findBydtkc
	 * @return
	 */
	public List<CurriculumTableForm> findTr(List<CurriculumTableForm> findBydtkc) {
		
		int datatr = 1;
		int datatri = 0;
		int daytr = 1;
		int dayi = 0;
		int timetr = 1;
		int timei = 0;
		for (int i = 0; i < findBydtkc.size(); i++) {
			CurriculumTableForm c = findBydtkc.get(i);
			/** 处理日期 **/
			if(c.getData()==null&&i!=0) {
				datatr ++;
			}else {
				findBydtkc.get(datatri).setDatatr(datatr);
				datatri = i;
				datatr = 1;
			}
			
			/** 处理上下午day **/
			if(c.getDay()==null&&i!=0) {
				daytr ++;
			}else {
				findBydtkc.get(dayi).setDaytr(daytr);
				dayi = i;
				daytr = 1;
			}
			/** 处理时间time **/
			if(c.getTime()==null&&i!=0) {
				timetr ++;
			}else {
				findBydtkc.get(timei).setTimetr(timetr);
				timei = i;
				timetr = 1;
			}
			
			if(i == findBydtkc.size()-1) {
				findBydtkc.get(datatri).setDatatr(datatr);
				findBydtkc.get(dayi).setDaytr(daytr);
				findBydtkc.get(timei).setTimetr(timetr);
			}
			
		}
		return findBydtkc;
	}

}
